<table width="100%"><tr><td width="20%">
<a href="../index.html">&lt; index</a><br />
<a href="index.html">&lt; 7. File parser</a><br />
<a href="parser_custom.html">&lt; 7.5 Using custom data types</a><br />
</td><td width="60%">
<p align="center">
=====================================<br />
7.6 Using the default parser listener<br />
=====================================<br />
</p></td><td width="20%">
</td></tr></table>
<style type="text/css">
.code { color:#444444; background-color:#EEEEEE;}
pre { color:#444444; background-color:#EEEEEE;}
table.param td { border : 1px solid #000000; }
th { background-color: #44BBFF; color: #FFFFFF }
table.none td { border : 0 }
</style>
You can run the parser with a default listener, by passing a NULL value in TCODParser::run :<br />
<p class="code"><pre>
C++ : myParser.run(filename,NULL)
C   : TCOD_parser_run(my_parser,filename,NULL)
Py  : parser_run(my_parser,filename,0)
</pre></p>
<br />
In that case, a default listener will be used and the properties read from the file will be stored in the parser. The default listener does not support multiple values, each property name must appear only once in the file, else the last property will overwrite the value of the previous, so it is mean to be use with basic config files.<br />
Using the default listener does not exempt you from declaring the structures in the parser. It still needs them to check the file validity. Once you run the parser, you can retrieve properties with following functions :<br />
<p class="code"><pre>
C++ : bool TCODParser::getBoolProperty(const char *name) const
      int TCODParser::getCharProperty(const char *name) const
      int TCODParser::getIntProperty(const char *name) const
      float TCODParser::getFloatProperty(const char *name) const
      TCODColor TCODParser::getColorProperty(const char *name) const
      TCOD_dice_t TCODParser::getDiceProperty(const char *name) const
      const char * TCODParser::getStringProperty(const char *name) const
      void * TCODParser::getCustomProperty(const char *name) const
      TCOD_list_t TCODParser::getListProperty(const char *name, TCOD_value_type_t type) const
C   : bool TCOD_parser_get_bool_property(TCOD_parser_t parser, const char *name)
      int TCOD_parser_get_char_property(TCOD_parser_t parser, const char *name)
      int TCOD_parser_get_int_property(TCOD_parser_t parser, const char *name)
      float TCOD_parser_get_float_property(TCOD_parser_t parser, const char *name)
      const char * TCOD_parser_get_string_property(TCOD_parser_t parser, const char *name)
      TCOD_color_t TCOD_parser_get_color_property(TCOD_parser_t parser, const char *name)
      TCOD_dice_t TCOD_parser_get_dice_property(TCOD_parser_t parser, const char *name)
      void * TCOD_parser_get_custom_property(TCOD_parser_t parser, const char *name)
      TCOD_list_t TCOD_parser_get_list_property(TCOD_parser_t parser, const char *name, TCOD_value_type_t type)
Py  : parser_get_bool_property(parser, name)
      parser_get_int_property(parser, name)
      parser_get_float_property(parser, name)
      parser_get_string_property(parser, name)
      parser_get_color_property(parser, name)
      parser_get_dice_property(parser, name)
      parser_get_list_property(parser, name, type)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>parser</td><td>In the C version, the parser handler, returned by <a href="#10.2.1">TCOD_parser_new</a>.</td></tr>
<tr><td>name</td><td>The full name of the property in the form &lt;structure&gt;.&lt;structure&gt;.&lt;propertyName&gt;. <tr><td>type</td><td>For the list properties, the type of the list's elements</td></tr>
</table>
Note that these function are rather slow especially if your configuration file is big, because they do a linear search in the property list, so you'd better not call them inside inner loops. If you try to get a property that was not declared in the parser, the program will exit with a fatal error. If an optional property is not defined in the file, the function will return 0 or equivalent depending on the type of the property (false for bool/flags, NULL for strings, black for color, an empty list for list properties).
<br />
Also note that TCODParser::getListProperty returns a C list structure, not a C++ template. Fortunately, TCODList has a constructor taking a TCOD_list_t as a parameter (see example below).<br />
Example, with this config file :<br />
<p class="code"><pre>
video {
	mode = "800x600"
        availableModes = [ "800x600", "1024x768", "1280x1024" ]
	fullscreen = false
}
input {
	mouse {
		sensitivity = 0.5
	}
}
</pre></p>
You can read those properties with following code :<br />
<p class="code"><pre>
C++ : char *mode=parser.getStringProperty("video.mode");
      TCODList&lt;char *&gt; availablesModes(parser.getListProperty("video.mode",TCOD_TYPE_STRING));
      bool fullscreen = parser.getBoolProperty("video.fullscreen");
      float mouseSensitivity = parser.getFloatProperty("input.mouse.sensitivity");
C   : char *mode=TCOD_parser_get_string_property(parser,"video.mode");
      TCOD_list_t availablesModes = TCOD_parser_get_list_property(parser,"video.mode",TCOD_TYPE_STRING));
      bool fullscreen = TCOD_parser_get_bool_property(parser,"video.fullscreen");
      float mouseSensitivity = TCOD_parser_get_float_property(parser,"input.mouse.sensitivity");
Py  : mode=litbcod.parser_get_string_property(parser,"video.mode")
      availablesModes = litbcod.parser_get_list_property(parser,"video.mode",litbcod.TYPE_STRING))
      fullscreen = litbcod.parser_get_bool_property(parser,"video.fullscreen")
      mouseSensitivity = litbcod.parser_get_float_property(parser,"input.mouse.sensitivity")
</pre></p>

